﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;

namespace PaymentHelper.Core.CommonUtils
{
    public static class PHUtility
    {
        public static string MD5(string input)
        {
            System.Security.Cryptography.MD5CryptoServiceProvider md5CryptService = new System.Security.Cryptography.MD5CryptoServiceProvider();

            byte[] bs = System.Text.Encoding.UTF8.GetBytes(input);

            bs = md5CryptService.ComputeHash(bs);

            System.Text.StringBuilder s = new System.Text.StringBuilder();

            foreach (byte b in bs)
            {
                s.Append(b.ToString("x2").ToLower());
            }

            return s.ToString();
        }

        public static IList<string> SplitToListString(this string str, char split)
        {
            string[] strArray = str.Split(split);
            IList<string> result = new List<string>();

            foreach (var s in strArray)
            {
                if (!string.IsNullOrEmpty(s))
                {
                    result.Add(s.Trim());
                }
            }

            return result;
        }

        public static string GetSiteUrl()
        {
            string host = HttpContext.Current.Request.Url.GetLeftPart(UriPartial.Authority) + HttpRuntime.AppDomainAppVirtualPath;

            host = host.Trim();
            int length = host.Length;

            if (host.EndsWith("/"))
            {
                host = host.Remove(length - 1, 1);
            }

            return host;
        }
    }
}
